import plugin from'../../../lib/plugins/plugin.js'
import{segment}from'oicq'
import fs from 'fs'
import YAML from 'yaml'
import Yaml from '../model/Yaml.js'
import cfg from'../../../lib/config/config.js'
import { Config} from '../components/index.js'
import common from'../../../lib/common/common.js'
const path=process.cwd()

let _path='./plugins/huasheng/config/只听我的_群号.yaml'
if (!fs.existsSync(_path)) {fs.writeFileSync(_path,'')}

const tp_path=path+'/plugins/huasheng/config/戳一戳/戳一戳图片/';

let pz_path=path+'/plugins/huasheng/config/戳一戳/戳一戳.yaml'

if (!fs.existsSync(tp_path)) {fs.mkdirSync(tp_path)}



export class hs_chuo extends plugin{
    constructor(){
    super({
        name: 'huasheng群聊戳一戳',
        dsc: '戳一戳',
        event: 'notice.group.poke',
        priority: -1,
        rule: [
            {
                /** 命令正则匹配 */
                fnc: 'chuoyichuo'
                }
            ]
        }
    )
}


async chuoyichuo (e){
if(!Config.getConfig('set','pz')['cyc']){ return false}
//判断只听我的
let group = await Yaml.getread(_path)
   try {
        for (let qqq of group) {
       if(e.group_id == qqq){
       if(e.operator_id ==cfg.masterQQ[0]){
          }else{
          return
          }
          }
        }
      } catch (e) {}

// if (await redis.get(`Yunzai:ShutUp${e.group_id}`)) {
            // return true
        // }
if(e.target_id == cfg.qq){
        //生成的随机数
        let i = Math.ceil(Math.random()*100)
        //获取数据
        let data=await Yaml.getread(pz_path)
        let 文字回复列表=data.戳一戳文字回复列表
        let 戳一戳是否撤回=data.戳一戳是否撤回
        let 戳一戳文字回复概率=data.戳一戳文字回复概率
        let 戳一戳其他回复概率=data.戳一戳其他回复概率
        let 戳一戳图片回复概率=data.戳一戳图片回复概率
        let 戳一戳禁言时间=data.戳一戳禁言时间
        let 戳一戳撤回时间=data.戳一戳撤回时间
        let 之和=戳一戳文字回复概率+戳一戳其他回复概率+戳一戳图片回复概率
        if(!(之和==100))
        {return logger.info('\x1B[36m戳一戳3个概率之和不等于100\x1B[0m')}
  
        //回复随机文字

   if(i < 戳一戳文字回复概率){
      
            let text_number = Math.ceil(Math.random() * 文字回复列表['length'])-1
          let az =  await e.reply(文字回复列表[text_number])
          if(戳一戳是否撤回){
            await common.sleep(戳一戳撤回时间);
           e.group.recallMsg(az.message_id);
         }
       }
        
        
         //回复随机图片
   else if(i < (戳一戳文字回复概率 + 戳一戳图片回复概率)){
        let photo_name=await fs.readdirSync(tp_path)
        // logger.info(photo_name)
        if(!photo_name['length']){return logger.info('\x1B[36m戳一戳图片目录没得图片\x1B[0m')}
        let photo_number = Math.ceil(Math.random() * photo_name['length'])-1
        let haha = await e.reply(segment.image(tp_path+photo_name[photo_number]))
        if(戳一戳是否撤回){
          await common.sleep(戳一戳撤回时间);
           e.group.recallMsg(haha.message_id);
           }
        }
   else{
let k= Math.ceil(Math.random()*4)
//禁言
if (k==4){
         let haha3 =   await  e.reply('不！！')
           await common.sleep(500);
         let haha4 =    await  e.reply('准！！')
           await common.sleep(500);
         let haha5 =    await   e.reply('戳！！')
           await common.sleep(1000);
        await e.group.muteMember(e.operator_id, 戳一戳禁言时间)
         let haha6 = await e.reply(segment.image(path+'/plugins/huasheng/resources/可莉语音/'+'sq.jpg'))
   if(戳一戳是否撤回){
  await common.sleep(戳一戳撤回时间);
  e.group.recallMsg(haha3.message_id);
  e.group.recallMsg(haha4.message_id);
  e.group.recallMsg(haha5.message_id);
  e.group.recallMsg(haha6.message_id);
        }
   }
//禁言2
  else if(k==3){    
  let heihei = await  e.reply('哼，我生气了！(◦`~´◦)')
  await e.group.muteMember(e.operator_id, 戳一戳禁言时间)
  if(戳一戳是否撤回){
  await common.sleep(戳一戳撤回时间);
  e.group.recallMsg(heihei.message_id);
   }
  }
//爬，表情
  else if(k==0||k==1){
    let har = await e.reply(await segment.image(`https://www.dreamling.xyz/API/qq_pa/api.php?qq=${e.operator_id}`))
    await e.group.muteMember(e.operator_id, 戳一戳禁言时间)
     if(戳一戳是否撤回){
            await common.sleep(戳一戳撤回时间);
            e.group.recallMsg(har.message_id);
           }
        }

//反击
  else if(k==2){
          let haha = await  e.reply('哼💢你戳我，我也要戳你！')
            await common.sleep(600)
            await e.group.pokeMember(e.operator_id)
            if(戳一戳是否撤回){
             await common.sleep(戳一戳撤回时间);
           e.group.recallMsg(haha.message_id);
         }
        }
        
     }
    
}
   return true
}
}



